package samples.spring;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;

/**
 * Bean生命周期方法
 * @author wangyuchuan
 * @date 2018/1/9
 */
public class LifeCycleBean implements BeanNameAware, BeanClassLoaderAware, BeanFactoryAware, EnvironmentAware,
    ResourceLoaderAware, ApplicationEventPublisherAware, MessageSourceAware, ApplicationContextAware, InitializingBean {
    public static final Logger log = LoggerFactory.getLogger(LifeCycleBean.class);

    public LifeCycleBean() {
        log.info("构造函数被调用...");
    }

    @Override
    public void setBeanName(String name) {
        log.info("Spring回调BeanNameAware's setBeanName invoked: {}", name);
    }

    @Override
    public void setBeanClassLoader(ClassLoader classLoader) {
        log.info("Spring回调BeanClassLoaderAware's setBeanClassLoader method invoked: {}", classLoader);
    }

    @Override
    public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
        log.info("Spring回调BeanFactoryAware's setBeanFactory method invoked: {}", beanFactory);
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        log.info("Spring回调ResourceLoaderAware's setResourceLoader method invoked: {}", resourceLoader);
    }

    @Override
    public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
        log.info("Spring回调ApplicationEventPublisherAware's setApplicationEventPublisher method invoked: {}", applicationEventPublisher);
    }

    @Override
    public void setMessageSource(MessageSource messageSource) {
        log.info("Spring回调MessageSourceAware's setMessageSource method invoked: {}", messageSource);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("Spring回调ApplicationContextAware's setApplicationContext method invoked: {}", applicationContext);
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        log.info("Spring回调InitializingBean's afterPropertiesSet method invoked:");
    }

    @Override
    public void setEnvironment(Environment environment) {
        log.info("Spring回调EnvironmentAware's setEnvironment method invoked: {}", environment);
    }

    public void initMehotd() {
        log.info("Spring回调initMehotd method invoked:");
    }
}
